<?php defined('BASEPATH') || exit('No direct script access allowed');

// 生成浏览页面全局ID
if (! function_exists('get_view_log_id')) {
    function get_view_log_id() {
        $_CI = &get_instance();
        $key = 'view_log_id';
        $_CI->load->driver('cache');
        return true;//$_CI->cache->redis->increment($key);
    }
}
// 生成浏览楼盘全局ID
if (! function_exists('get_view_building_log_id')) {
    function get_view_building_log_id() {
        $_CI = &get_instance();
        $key = 'view_building_log_id';
        $_CI->load->driver('cache');
        return true;//$_CI->cache->redis->increment($key);
    }
}
// 生成楼盘事件全局ID
if (! function_exists('get_building_event_log_id')) {
    function get_building_event_log_id() {
        $_CI = &get_instance();
        $key = 'building_event_log_id';
        $_CI->load->driver('cache');
        return true;//$_CI->cache->redis->increment($key);
    }
}


// 获取来自搜索引擎入站时的关键词 @TBD 代码需要优化
if (! function_exists('get_search_engine_keyword')) {
    function get_search_engine_keyword($domain, $path){
        if (empty($domain)) {
            return $arr = array(
                            'search_engine'=>'直接访问',
                            'keywords'=>'',
                          );
        }
        if(strpos($domain, 'google.com.tw')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'GOOGLE TAIWAN';
            $keywords = urldecode($regs[1]); // google taiwan
        }
        if(strpos($domain,'google.cn')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'GOOGLE CHINA';
            $keywords = urldecode($regs[1]); // google china
        }
        if(strpos($domain,'google.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'GOOGLE';
            $keywords = urldecode($regs[1]); // google
        }elseif(strpos($domain,'baidu.')!==false && preg_match('/wd=([^&]*)/i',$path,$regs)){
            $searchengine = 'baidu';
            $keywords = urldecode($regs[1]); // baidu
        }elseif(strpos($domain,'baidu.')!==false && preg_match('/word=([^&]*)/i',$path,$regs)){
            $searchengine = 'baidu';
            $keywords = urldecode($regs[1]); // baidu
        }elseif(strpos($domain,'114.vnet.cn')!== false && preg_match('/kw=([^&]*)/i',$path,$regs)){
            $searchengine = 'CT114';
            $keywords = urldecode($regs[1]); // ct114
        }elseif(strpos($domain,'iask.com')!==false && preg_match('/k=([^&]*)/i',$path,$regs)){
            $searchengine = 'IASK';
            $keywords = urldecode($regs[1]); // iask
        }elseif(strpos($domain,'soso.com')!==false && preg_match('/w=([^&]*)/i',$path,$regs)){
            $searchengine = 'SOSO';
            $keywords = urldecode($regs[1]); // soso
        }elseif(strpos($domain, 'sogou.com')!==false && preg_match('/query=([^&]*)/i',$path,$regs)){
            $searchengine = 'sogou';
            $keywords = urldecode($regs[1]); // sogou
        }elseif(strpos($domain,'so.163.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'NETEASE';
            $keywords = urldecode($regs[1]); // netease
        }elseif(strpos($domain,'yodao.com')!== false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'yodao';
            $keywords = urldecode($regs[1]); // yodao
        }elseif(strpos($domain,'zhongsou.com')!==false && preg_match('/word=([^&]*)/i',$path,$regs)){
            $searchengine = 'ZHONGSOU';
            $keywords = urldecode($regs[1]); // zhongsou
        }elseif(strpos($domain,'search.tom.com')!==false && preg_match('/w=([^&]*)/i',$path,$regs)){
            $searchengine = 'TOM';
            $keywords = urldecode($regs[1]); // tom
        }elseif(strpos($domain,'live.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'MSLIVE';
            $keywords = urldecode($regs[1]); // MSLIVE
        }elseif(strpos($domain, 'tw.search.yahoo.com')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){
            $searchengine = 'YAHOO TAIWAN';
            $keywords = urldecode($regs[1]); // yahoo taiwan
        }elseif(strpos($domain,'cn.yahoo.')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){
            $searchengine = 'YAHOO CHINA';
            $keywords = urldecode($regs[1]); // yahoo china
        }elseif(strpos($domain,'yahoo.')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){
            $searchengine = 'YAHOO';
            $keywords = urldecode($regs[1]); // yahoo
        }elseif(strpos($domain,'msn.com.tw')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'MSN TAIWAN';
            $keywords = urldecode($regs[1]); // msn taiwan
        }elseif(strpos($domain,'msn.com.cn')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'MSN CHINA';
            $keywords = urldecode($regs[1]); // msn china
        }elseif(strpos($domain,'msn.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'MSN';
            $keywords = urldecode($regs[1]); // msn
        }elseif(strpos($domain,'haosou.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'haosou';
            $keywords = urldecode($regs[1]); 
        }elseif(strpos($domain,'bing.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'bing';
            $keywords = urldecode($regs[1]); 
        }elseif(strpos($domain,'chinaso.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){
            $searchengine = 'chinaso';
            $keywords = urldecode($regs[1]); 
        }else{
            $searchengine = '来源连接访问';
            $keywords = '';
        }
        $arr = array(
          'search_engine'=>$searchengine,
          'keywords'=>$keywords,
        );
        return $arr;
    } 

}

// 判断是否 com 或者 cn 域名访问
if (! function_exists('is_com_server_name')) {
    function is_com_server_name() {
        $serverName = $_SERVER['HTTP_HOST'];
        if (stripos($serverName, '.cn') !== false) {
            return false;
        } else if (stripos($serverName, '.com') !== false) {
            return true;
        }
        return false;
    }
}

// 是否是 udfang.com 域名访问
if (!function_exists('is_udfang_server_name')) {
    function is_udfang_server_name() {
        $serverName = $_SERVER['HTTP_HOST'];
        if (stripos($serverName, 'udfang.com') !== false) {
            return true;
        }
        return false;
    }
}

// 是否是 fang90.com 域名访问
if (!function_exists('is_fang90_server_name')) {
    function is_fang90_server_name() {
        $serverName = $_SERVER['HTTP_HOST'];
        if (stripos($serverName, 'fang90.com') !== false) {
            return true;
        }
        return is_plusfang_server_name();
        // return false;
    }
}
// 是否是 http://www.pinxiaochina.cn/ 域名访问
if (!function_exists('is_pinxiaochina_server_name')) {
    function is_pinxiaochina_server_name() {
        $serverName = $_SERVER['HTTP_HOST'];
        if (stripos($serverName, 'www.pinxiaochina.cn') !== false) {
            return true;
        }
        return false;
    }
}
// 是否是 plus-fang.com 域名访问
if (!function_exists('is_plusfang_server_name')) {
    function is_plusfang_server_name() {
        $serverName = $_SERVER['HTTP_HOST'];
        if (stripos($serverName, 'plus-fang.com') !== false) {
            return true;
        }
        return false;
    }
}

if (!function_exists('int_to_cn')) {
    /**
     * 数字转中文
     * @param Int $var 需要解析的数字
     * @param String $start 初始值
     * @return String
     * @author Anyon Zou <zoujingli@echounion.com>
     * @date 2013-08-22 01:20
     */
    function int_to_cn($num, $string = array()) {
        if (!is_numeric($num)) {
            return $num;
        }
        $splits = array('100000000' => '亿', '10000' => '万');
        $chars = array('10000' => '万', '1000' => '千', '100' => '百', '10' => '十', '1' => '', '0' => '零');
        $ints = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十');

        foreach ($splits as $step => $split) {
            $floor = floor($num / $step);
            if ($floor > 0) {
                $string[] = int_to_cn($floor) . $split;
                $num = fmod($num, $step);
            }
        }
        $string2 = array();
        foreach ($chars as $step => $char) {
            $floor = @floor($num / $step);
            if ($floor > 0) {
                $string[] = $string2[] = $ints[$floor] . $char;
                $num = fmod($num, $step);
            } else if ((count($string2) > 0 || (count($string) > 0 && $step != '10000')) && $string2[count($string) - 1] != $ints[0] && $num > 0) {
                $string[] = $ints[0];
            }
        }
        return join('', $string);
    }
}

if (!function_exists('view_event_log')) {

    function view_event_log($data) {
        if (empty($data)) return false;
        
        $path = APPPATH.'logs'.DIRECTORY_SEPARATOR.'views';
        file_exists($path) OR mkdir($path, 0755, TRUE);

        $filename = 'view_'.date("Y-m-d").'.txt';
        return file_put_contents($path.DIRECTORY_SEPARATOR.$filename, $data.PHP_EOL, FILE_APPEND | LOCK_EX);
    }

}
if (!function_exists('view_html5_log')) {

    function view_html5_log($data) {
        if (empty($data)) return false;
        
        $path = APPPATH.'logs'.DIRECTORY_SEPARATOR.'html5';
        file_exists($path) OR mkdir($path, 0755, TRUE);

        $filename = 'view_h5_'.date("Y-m-d").'.txt';
        return file_put_contents($path.DIRECTORY_SEPARATOR.$filename, $data.PHP_EOL, FILE_APPEND | LOCK_EX);
    }

}

?>